package com.uzai.console.service.wxpub.impl;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.dto.wxpub.activecode.*;
import com.uzai.console.dto.wxpubmanage.QrcodeDto;
import com.uzai.console.dto.wxpubmanage.WxpubInfoListQuery;
import com.uzai.console.entity.DeviceMiniInfo;
import com.uzai.console.entity.MiniComponentAuth;
import com.uzai.console.entity.WxpubActiveCode;
import com.uzai.console.entity.WxpubActiveCodeDetail;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.mapper.WxpubActiveCodeDetailMapper;
import com.uzai.console.mapper.WxpubActiveCodeMapper;
import com.uzai.console.mapper.WxpubInfoMapper;
import com.uzai.console.service.feign.WxServerFeignService;
import com.uzai.console.service.wxpub.WxpubActiveCodeService;
import com.uzai.console.service.wxpubmanage.WxpubInfoService;
import com.uzai.console.vo.wxpub.CreateQrcodeVo;
import com.uzai.console.vo.wxpub.activecode.WxpubActiveCodeDetailVo;
import com.uzai.console.vo.wxpub.activecode.WxpubActiveCodeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class WxpubActiveCodeServiceImpl implements WxpubActiveCodeService {

    Logger logger = LoggerFactory.getLogger(WxpubActiveCodeServiceImpl.class);

    @Autowired
    private WxpubActiveCodeMapper wxpubActiveCodeMapper;
    @Autowired
    private WxpubInfoMapper wxpubInfoMapper;
    @Autowired
    private WxpubActiveCodeDetailMapper wxpubActiveCodeDetailMapper;
    @Autowired
    private WxServerFeignService wxServerFeignService;
    @Autowired
    private WxpubInfoService wxpubInfoService;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 分页查询会员类型列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeQuery
     * @return null
     */
    public Page<WxpubActiveCodeVo> findWxpubActiveCodePage(WxpubActiveCodeQuery wxpubActiveCodeQuery){
        //新建返回到页面的LIST对象
        List<WxpubActiveCodeVo> wxpubActiveCodeInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<WxpubActiveCode> page = new Page<>();
        wxpubActiveCodeQuery.convert(page);
        List<WxpubActiveCode> wxpubActiveCodeList = wxpubActiveCodeMapper.findByList(page, wxpubActiveCodeQuery);

        if(wxpubActiveCodeList != null){
            for (WxpubActiveCode wxpubActiveCode : wxpubActiveCodeList){
                WxpubActiveCodeVo wxpubActiveCodeInfoVo = new WxpubActiveCodeVo();
                BeanUtils.copyProperties(wxpubActiveCode, wxpubActiveCodeInfoVo);
                wxpubActiveCodeInfoVoList.add(wxpubActiveCodeInfoVo);
            }
        }

        //返回到页面分页对象
        Page<WxpubActiveCodeVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(wxpubActiveCodeInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询会员类型详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeIdDto
     * @return null
     */
    public WxpubActiveCodeVo findWxpubActiveCode(WxpubActiveCodeIdDto wxpubActiveCodeIdDto){
        if(wxpubActiveCodeIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        WxpubActiveCode wxpubActiveCode = wxpubActiveCodeMapper.selectById(wxpubActiveCodeIdDto.getId(), wxpubActiveCodeIdDto.getMerId());

        if(wxpubActiveCode == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        WxpubActiveCodeVo wxpubActiveCodeInfoVo =  new WxpubActiveCodeVo();

        BeanUtils.copyProperties(wxpubActiveCode, wxpubActiveCodeInfoVo);

        return wxpubActiveCodeInfoVo;
    }

    /**
     * 添加公众号活码
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeDto
     * @return null
     */
    @Transactional
    public void addWxpubActiveCode(WxpubActiveCodeDto wxpubActiveCodeDto){
        if(wxpubActiveCodeDto.getWxpubId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择公众号");
        }

        WxpubInfoEntity wxpubInfoEntity = wxpubInfoMapper.queryObject(wxpubActiveCodeDto.getWxpubId());
        if(wxpubInfoEntity == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该公众号不存在");
        }

        if(StringUtils.isBlank(wxpubActiveCodeDto.getActiveCode())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入活码名称");
        }

        if(wxpubActiveCodeDto.getDaySendMax() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入活码当天上限");
        }

        if(wxpubActiveCodeDto.getTotalSendMax() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入活码总上限");
        }

        List<WxpubActiveCodeDetailDto> detailList = wxpubActiveCodeDto.getDetailList();
        if(detailList == null || detailList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入二维码列表");
        }

        //当前时间
        int now = DateUtil.getNowTime();

        //添加活码
        WxpubActiveCode wxpubActiveCode = new WxpubActiveCode();
        wxpubActiveCode.setId(IdWorker.getId());
        wxpubActiveCode.setMerId(wxpubActiveCodeDto.getMerId());
        wxpubActiveCode.setWxpubId(wxpubActiveCodeDto.getWxpubId());
        wxpubActiveCode.setWxpubName(wxpubInfoEntity.getDeviceName());
        wxpubActiveCode.setActiveCode(wxpubActiveCodeDto.getActiveCode());
        wxpubActiveCode.setRemark(wxpubActiveCodeDto.getRemark());
        wxpubActiveCode.setCreateTime(now);
        wxpubActiveCode.setUpdateTime(now);
        wxpubActiveCodeMapper.insertSelective(wxpubActiveCode);

        //活码明细
        for (WxpubActiveCodeDetailDto detail : detailList) {
            WxpubActiveCodeDetail wxpubActiveCodeDetail = new WxpubActiveCodeDetail();
            wxpubActiveCodeDetail.setId(IdWorker.getId());
            wxpubActiveCodeDetail.setMerId(wxpubActiveCode.getMerId());
            wxpubActiveCodeDetail.setWxpubId(wxpubActiveCode.getWxpubId());
            wxpubActiveCodeDetail.setActiveCodeId(wxpubActiveCode.getId());
            wxpubActiveCodeDetail.setImgUrl(detail.getImgUrl());
            wxpubActiveCodeDetail.setImgName(detail.getImgName());
            wxpubActiveCodeDetail.setRemark(detail.getRemark());
            wxpubActiveCodeDetail.setDaySendCount(Tools.getInteger(wxpubActiveCodeDto.getDaySendCount()));
            wxpubActiveCodeDetail.setDaySendMax(Tools.getInteger(wxpubActiveCodeDto.getDaySendMax()));
            wxpubActiveCodeDetail.setTotalSendCount(Tools.getInteger(wxpubActiveCodeDto.getTotalSendCount()));
            wxpubActiveCodeDetail.setTotalSendMax(Tools.getInteger(wxpubActiveCodeDto.getTotalSendMax()));
            wxpubActiveCodeDetail.setPageOrder(1); //默认排序1
            wxpubActiveCodeDetail.setCreateTime(now);
            wxpubActiveCodeDetail.setUpdateTime(now);
            wxpubActiveCodeDetailMapper.insertSelective(wxpubActiveCodeDetail);
        }
    }

    /**
     * 追加公众号活码
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeDto
     * @return null
     */
    @Transactional
    public void additionalWxpubActiveCode(WxpubActiveCodeDto wxpubActiveCodeDto){
        if(wxpubActiveCodeDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择追加的活码id");
        }

        WxpubActiveCode wxpubActiveCode = wxpubActiveCodeMapper.selectById(wxpubActiveCodeDto.getId(), wxpubActiveCodeDto.getMerId());
        if(wxpubActiveCode == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该活码不存在");
        }

        List<WxpubActiveCodeDetailDto> detailList = wxpubActiveCodeDto.getDetailList();
        if(detailList == null || detailList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入二维码列表");
        }

        if(wxpubActiveCodeDto.getDaySendMax() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入活码当天上限");
        }

        if(wxpubActiveCodeDto.getTotalSendMax() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入活码总上限");
        }

        //当前时间
        int now = DateUtil.getNowTime();

        //活码追加明细
        for (WxpubActiveCodeDetailDto detail : detailList) {
            WxpubActiveCodeDetail wxpubActiveCodeDetail = new WxpubActiveCodeDetail();
            wxpubActiveCodeDetail.setId(IdWorker.getId());
            wxpubActiveCodeDetail.setMerId(wxpubActiveCode.getMerId());
            wxpubActiveCodeDetail.setWxpubId(wxpubActiveCode.getWxpubId());
            wxpubActiveCodeDetail.setActiveCodeId(wxpubActiveCode.getId());
            wxpubActiveCodeDetail.setImgUrl(detail.getImgUrl());
            wxpubActiveCodeDetail.setImgName(detail.getImgName());
            wxpubActiveCodeDetail.setRemark(detail.getRemark());
            wxpubActiveCodeDetail.setDaySendCount(Tools.getInteger(wxpubActiveCodeDto.getDaySendCount()));
            wxpubActiveCodeDetail.setDaySendMax(Tools.getInteger(wxpubActiveCodeDto.getDaySendMax()));
            wxpubActiveCodeDetail.setTotalSendCount(Tools.getInteger(wxpubActiveCodeDto.getTotalSendCount()));
            wxpubActiveCodeDetail.setTotalSendMax(Tools.getInteger(wxpubActiveCodeDto.getTotalSendMax()));
            wxpubActiveCodeDetail.setPageOrder(1); //默认排序1
            wxpubActiveCodeDetail.setCreateTime(now);
            wxpubActiveCodeDetail.setUpdateTime(now);
            wxpubActiveCodeDetailMapper.insertSelective(wxpubActiveCodeDetail);
        }
    }

    /**
     * 修改活码的公众号
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param updateWxpubDto
     * @return null
     */
    public void updateWxpub(UpdateWxpubDto updateWxpubDto){

        if(updateWxpubDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }

        WxpubActiveCode wxpubActiveCode = wxpubActiveCodeMapper.selectById(updateWxpubDto.getId(), updateWxpubDto.getMerId());
        if(wxpubActiveCode == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该活码不存在");
        }

        if(updateWxpubDto.getWxpubId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择公众号");
        }

        WxpubInfoEntity wxpubInfoEntity = wxpubInfoMapper.queryObject(updateWxpubDto.getWxpubId());
        if(wxpubInfoEntity == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该公众号不存在");
        }

        wxpubActiveCode.setWxpubId(updateWxpubDto.getWxpubId());
        wxpubActiveCode.setWxpubName(wxpubInfoEntity.getDeviceName());
        wxpubActiveCode.setUpdateTime(DateUtil.getNowTime());
        wxpubActiveCodeMapper.updateByIdSelective(wxpubActiveCode);

    }

    /**
     * 修改活码
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param updateActiveCodeNameDto
     * @return null
     */
    public void updateActiveCodeName(UpdateActiveCodeNameDto updateActiveCodeNameDto){
        if(updateActiveCodeNameDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }

        WxpubActiveCode wxpubActiveCode = wxpubActiveCodeMapper.selectById(updateActiveCodeNameDto.getId(), updateActiveCodeNameDto.getMerId());
        if(wxpubActiveCode == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该活码不存在");
        }

        if(StringUtils.isBlank(updateActiveCodeNameDto.getActiveCode())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入活码名称");
        }

        wxpubActiveCode.setActiveCode(updateActiveCodeNameDto.getActiveCode());
        wxpubActiveCode.setUpdateTime(DateUtil.getNowTime());
        wxpubActiveCodeMapper.updateByIdSelective(wxpubActiveCode);
    }

    /**
     * 删除活码
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeIdDto
     * @return null
     */
    @Transactional
    public void deleteActiveCode(WxpubActiveCodeIdDto wxpubActiveCodeIdDto){
        if(wxpubActiveCodeIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }

        WxpubActiveCode wxpubActiveCode = wxpubActiveCodeMapper.selectById(wxpubActiveCodeIdDto.getId(), wxpubActiveCodeIdDto.getMerId());
        if(wxpubActiveCode == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该活码不存在");
        }

        //删除活码
        wxpubActiveCodeMapper.deleteById(wxpubActiveCodeIdDto.getId(), wxpubActiveCodeIdDto.getMerId());

        //删除活码明细
        wxpubActiveCodeDetailMapper.deleteByActiveCodeId(wxpubActiveCodeIdDto.getId(), wxpubActiveCodeIdDto.getMerId());
    }

    /**
     * 查询所有公众号活码明细列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeDetailQuery
     * @return null
     */
    public Page<WxpubActiveCodeDetailVo> findWxpubActiveCodeDetailPage(WxpubActiveCodeDetailQuery wxpubActiveCodeDetailQuery){
        //新建返回到页面的LIST对象
        List<WxpubActiveCodeDetailVo> WxpubActiveCodeDetailVoList = new ArrayList<>();

        //执行分页查询对象
        Page<WxpubActiveCodeDetail> page = new Page<>();
        wxpubActiveCodeDetailQuery.convert(page);
        List<WxpubActiveCodeDetail> wxpubActiveCodeDetailList = wxpubActiveCodeDetailMapper.findByList(page, wxpubActiveCodeDetailQuery);

        if(wxpubActiveCodeDetailList != null){
            for (WxpubActiveCodeDetail wxpubActiveCodeDetail : wxpubActiveCodeDetailList){
                WxpubActiveCodeDetailVo wxpubActiveCodeDetailVo = new WxpubActiveCodeDetailVo();
                BeanUtils.copyProperties(wxpubActiveCodeDetail, wxpubActiveCodeDetailVo);
                WxpubActiveCodeDetailVoList.add(wxpubActiveCodeDetailVo);
            }
        }

        //返回到页面分页对象
        Page<WxpubActiveCodeDetailVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(WxpubActiveCodeDetailVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询公众号活码明细详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeDetailIdDto
     * @return null
     */
    public WxpubActiveCodeDetailVo findWxpubActiveCodeDetail(WxpubActiveCodeDetailIdDto wxpubActiveCodeDetailIdDto){
        if(wxpubActiveCodeDetailIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        WxpubActiveCodeDetail wxpubActiveCodeDetail = wxpubActiveCodeDetailMapper.selectById(wxpubActiveCodeDetailIdDto.getId(), wxpubActiveCodeDetailIdDto.getMerId());

        if(wxpubActiveCodeDetail == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        WxpubActiveCodeDetailVo wxpubActiveCodeDetailVo =  new WxpubActiveCodeDetailVo();

        BeanUtils.copyProperties(wxpubActiveCodeDetail, wxpubActiveCodeDetailVo);

        return wxpubActiveCodeDetailVo;
    }

    /**
     * 修改公众号活码明细详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeDetailDto
     * @return null
     */
    public void updateWxpubActiveCodeDetail(WxpubActiveCodeDetailDto wxpubActiveCodeDetailDto){
        if(wxpubActiveCodeDetailDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        WxpubActiveCodeDetail wxpubActiveCodeDetail = wxpubActiveCodeDetailMapper.selectById(wxpubActiveCodeDetailDto.getId(), wxpubActiveCodeDetailDto.getMerId());

        if(wxpubActiveCodeDetail == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        wxpubActiveCodeDetail.setDaySendCount(Tools.getInteger(wxpubActiveCodeDetailDto.getDaySendCount()));
        wxpubActiveCodeDetail.setDaySendMax(Tools.getInteger(wxpubActiveCodeDetailDto.getDaySendMax()));
        wxpubActiveCodeDetail.setTotalSendCount(Tools.getInteger(wxpubActiveCodeDetailDto.getTotalSendCount()));
        wxpubActiveCodeDetail.setTotalSendMax(Tools.getInteger(wxpubActiveCodeDetailDto.getTotalSendMax()));
        wxpubActiveCodeDetail.setRemark(wxpubActiveCodeDetailDto.getRemark());
        wxpubActiveCodeDetail.setUpdateTime(DateUtil.getNowTime());
        wxpubActiveCodeDetailMapper.updateById(wxpubActiveCodeDetail);

    }

    /**
     * 修改公众号活码页面排序
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param updatePageOrderDto
     * @return null
     */
    public void updatePageOrder(UpdatePageOrderDto updatePageOrderDto){
        if(updatePageOrderDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        WxpubActiveCodeDetail wxpubActiveCodeDetail = wxpubActiveCodeDetailMapper.selectById(updatePageOrderDto.getId(), updatePageOrderDto.getMerId());

        if(wxpubActiveCodeDetail == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        wxpubActiveCodeDetail.setPageOrder(Tools.getInteger(updatePageOrderDto.getPageOrder()));
        wxpubActiveCodeDetail.setUpdateTime(DateUtil.getNowTime());
        wxpubActiveCodeDetailMapper.updateById(wxpubActiveCodeDetail);
    }

    /**
     * 删除活码明细
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeDetailIdDto
     * @return null
     */
    public void deleteActiveCodeDetail(WxpubActiveCodeDetailIdDto wxpubActiveCodeDetailIdDto){
        if(wxpubActiveCodeDetailIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        WxpubActiveCodeDetail wxpubActiveCodeDetail = wxpubActiveCodeDetailMapper.selectById(wxpubActiveCodeDetailIdDto.getId(), wxpubActiveCodeDetailIdDto.getMerId());

        if(wxpubActiveCodeDetail == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        wxpubActiveCodeDetailMapper.deleteById(wxpubActiveCodeDetailIdDto.getId(), wxpubActiveCodeDetailIdDto.getMerId());
    }

    /**
     * 查询公众号列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param baseDto
     * @return null
     */
    public List<WxpubInfoEntity> findWxpubList(BaseDto baseDto){

        WxpubInfoListQuery query = new WxpubInfoListQuery();
        query.setMerId(baseDto.getMerId());
        List<WxpubInfoEntity> wxpubInfoEntityList = wxpubInfoMapper.queryList(query);
        return wxpubInfoEntityList;
    }

    /**
     * 获取带参二维码
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxpubActiveCodeIdDto
     * @return null
     */
    public String createQrcode(WxpubActiveCodeIdDto wxpubActiveCodeIdDto){

        String qrcode_encodeStr = null;

        if(wxpubActiveCodeIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        WxpubActiveCode wxpubActiveCode = wxpubActiveCodeMapper.selectById(wxpubActiveCodeIdDto.getId(), wxpubActiveCodeIdDto.getMerId());
        if(wxpubActiveCode == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该活码不存在");
        }

        QrcodeDto dto = new QrcodeDto();
        dto.setMerId(wxpubActiveCodeIdDto.getMerId());
        dto.setDeviceUniqueId(wxpubActiveCode.getWxpubId());
        dto.setSceneId(wxpubActiveCode.getId());
        UzaiRespVo<CreateQrcodeVo> uzaiRespVo_ticket = wxServerFeignService.createQrcode(dto);

        logger.info("调用创建活码二维码接口，para={},result={}", JSONObject.toJSONString(dto), JSONObject.toJSONString(uzaiRespVo_ticket));

        //授权码
        String ticket = null;
        if(uzaiRespVo_ticket != null && uzaiRespVo_ticket.getCode() == 200){
            CreateQrcodeVo createQrcodeVo = uzaiRespVo_ticket.getData();
            if(createQrcodeVo != null){
                ticket = createQrcodeVo.getTicket();
            }
        }

        if(Strings.isBlank(ticket)){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未获取到ticket授权");
        }

        //获取二维码
        String get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;
        // 定义文件下载的URL
        // 发送HTTP GET请求并接收响应
        ResponseEntity<byte[]> response = restTemplate.exchange(get_url, HttpMethod.GET, null, byte[].class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            logger.info("--createQrcode-- body={}", response.getBody());
            byte[] data = response.getBody();
            String encodeStr = Base64.encode(data);
            qrcode_encodeStr = "data:image/jpeg;base64," + encodeStr;
        } else {
            logger.info("--createQrcode-code=" + (response == null ? "" : response.getStatusCodeValue()));
        }

        if(Strings.isBlank(qrcode_encodeStr)){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "创建带参二维码失败");
        }

        wxpubActiveCode.setQrcode(qrcode_encodeStr);
        wxpubActiveCode.setUpdateTime(DateUtil.getNowTime());
        wxpubActiveCodeMapper.updateByIdSelective(wxpubActiveCode);

        return qrcode_encodeStr;
    }


}
